package com.lfz.demo.common.config;

/**
 * @author lfz
 * @date 2025/3/14 9:19
 */
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Configuration
public class DateFormatConfig {

    @Bean
    public Converter<String, Date> dateConverter() {
        return new Converter<String, Date>() {
            @Override
            public Date convert(String source) {
                if (source == null || source.isEmpty()) {
                    return null;
                }
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                try {
                    return sdf.parse(source);
                } catch (ParseException e) {
                    throw new IllegalArgumentException("日期格式不正确，请使用 yyyy-MM-dd HH:mm:ss 格式", e);
                }
            }
        };
    }
}
